package activitys;

import java.util.List;

import com.example.pasajero.R;
import com.example.pasajero.RegistroFavoritoActivity;
import com.google.gson.Gson;

import controladores.ControladorPasajeros;

import utiles.MyAdapter;
import utils.PreferencesHelper;

import entidades.Favorito;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class FavoritosActivity extends Activity
{	
	private List<Favorito> favoritos;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_favoritos);

		Log.i("LOG", "Favoritos Activity");
		
		favoritos = null;
		
	}

	@Override
	public void onResume()
	{
		super.onResume(); 

		try
		{
			favoritos = new SolicitarFavoritos().execute().get();
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (favoritos != null)
		{
			MyAdapter ad = new MyAdapter(this, favoritos);

			ListView lista = (ListView) findViewById(R.id.listView_Favoritos);
			lista.setAdapter(ad);
			lista.setOnItemClickListener(new MyListener(this, favoritos));
		}

	}
	
	
	public void returnToCaller(Favorito favorito)
	{
		String key = "clave";
		Gson gson = new Gson();

		Bundle bund = new Bundle();
		bund.putString(key, gson.toJson(favorito));
		Intent i;
		i = new Intent();
		i.putExtras(bund);
		setResult(0, i);
		finish();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.favoritos, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpTo(this, new Intent(this, MainActivity.class));
            return true;

        case R.id.action_add_item:
            addItem();
            return true;
    }
		return super.onOptionsItemSelected(item);
	}

	private void addItem()
	{
		Intent i = new Intent(this,RegistroFavoritoActivity.class);
		startActivity(i);
	}

	private class MyListener implements OnItemClickListener
	{
		private Context context;

		public MyListener(Context context, List<Favorito> items)
		{
			this.context = context;
		}

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long seleccion)
		{
			try
			{
				returnToCaller(favoritos.get((int) seleccion));
			} catch (NullPointerException ex)
			{
				Log.i("LOG", "" + ex.getLocalizedMessage());
			}
		}

	}

	private class SolicitarFavoritos extends
			AsyncTask<Void, String, List<Favorito>>
	{
		private ProgressDialog progress;

		public SolicitarFavoritos()
		{
			progress = new ProgressDialog(FavoritosActivity.this);
			progress.setMessage("Cargando...");
			
			progress.setCancelable(true);
			progress.setCanceledOnTouchOutside(true);
		}

		@Override
		protected void onPreExecute()
		{
			// TODO código del onPreExecute (Hilo Principal)
			progress.show();
		}

		@Override
		protected List<Favorito> doInBackground(Void... id)
		{
			return new ControladorPasajeros().getFavoritos(new PreferencesHelper(getApplicationContext()).leerID());
		}

		@Override
		protected void onProgressUpdate(String... varDuranteBackground)
		{
			// TODO código del onProgressUpdate (Hilo Principal)
		}

		@Override
		protected void onPostExecute(List<Favorito> varTerminarBackground)
		{
			// TODO código del onPostExecute (Hilo Principal)
			progress.dismiss();
		}

		@Override
		protected void onCancelled(List<Favorito> varTerminarBackground)
		{
			// TODO código del onCancelled (Hilo Principal)
		}

	}
}
